package com.jiazhong.boot.example.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.boot.commons.bean.Emp;
import com.jiazhong.boot.commons.util.JsonResult;
import com.jiazhong.boot.commons.util.ResultTool;
import com.jiazhong.boot.example.emp.mapper.EmpMapper;
import com.jiazhong.boot.example.emp.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper mapper;

    @Override
    public JsonResult find(int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> list = mapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult findAllMgr() {
        return ResultTool.success(mapper.findAllMgr());
    }

    @Override
    public JsonResult findAllJob() {
        return ResultTool.success(mapper.findAllJob());
    }

    @Override
    public JsonResult findEnameExists(String ename) {
        int count = mapper.findEnameCount(ename);
        if (count > 0) {
            return ResultTool.fail(500, "姓名已经存在");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult save(Emp emp) {
        int row = mapper.save(emp);
        if (row == 0) {
            return ResultTool.fail(500, "添加员工失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult removeById(int id) {
        int row = mapper.removeById(id);
        if (row == 0) {
            return ResultTool.fail(500, "删除员工失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult removeByBatch(String ids) {
        String[] idArr = ids.split(",");
        List<String> list = Arrays.asList(idArr);
        int row = mapper.removeByBatch(list);
        if (row == 0) {
            return ResultTool.fail(500, "批量删除员工失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult update(Emp emp) {
        int row = mapper.update(emp);
        if (row == 0) {
            return ResultTool.fail(500, "修改员工信息失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findCascade(Emp emp, int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> list = mapper.findCascade(emp);
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }
}
